
use alloc::sync::Arc;
use core::cell::RefCell;
use ostd::{mm::VmSpace, task::CurrentTask};

pub type EndPoint = u64;

pub struct ThreadLocal {
    vmspace: RefCell<Option<Arc<VmSpace>>>,

    endpoint: EndPoint,
}
impl ThreadLocal {
    pub fn new(vmspace: Arc<VmSpace>) -> Self {
        Self {
            vmspace: RefCell::new(Some(vmspace)),
            endpoint: 0, // TODO: generate endpoint
        }
    }
    pub fn vm_space(&self) -> &RefCell<Option<Arc<VmSpace>>> {
        &self.vmspace
    }
}

pub trait AsThreadLocal {
    fn as_thread_local(&self) -> Option<&ThreadLocal>;
}

impl AsThreadLocal for CurrentTask {
    fn as_thread_local(&self) -> Option<&ThreadLocal> {
        self.local_data().downcast_ref()
    }
}